using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using QuanLyDoanVien.Controller;
using QuanLyDoanVien.Bussiness;
using QuanLyDoanVien.General;
using QuanLyDoanVien.DataLayer;
using DevComponents.DotNetBar.Controls;

namespace QuanLyDoanVien
{
    public partial class frmQLLop : DevComponents.DotNetBar.Office2007Form
    {
        QuyDinh m_QuyDinh = new QuyDinh();
        LopData m_LopData = new LopData();
        LopCtrl m_LopCtrl = new LopCtrl();
        List<LopInfo> dslop = new List<LopInfo>();
        
        public frmQLLop()
        {
            InitializeComponent();
            DataService.OpenConnection();
        }

        

        private LopInfo New_Lop()
        {
            String malop = m_LopCtrl.getAutoID();
            LopInfo lop = new LopInfo(malop, txt_L_Ten.Text, txt_L_Chuyen.Text);
            return lop;
        }
        private void resetForm()
        {
            this.txt_L_Ma.Text = "";
            this.txt_L_Ten.Text = "";
            this.txt_L_Chuyen.Text = "";
        }
       
        private void btn_L_Them_Click(object sender, EventArgs e)
        {
            LopInfo lop = New_Lop();
            m_LopData.LuuLop(lop);
            loadDSLop();
            this.resetForm();

        }
        private void loadDSLop()
        {
            dataGridView_L.Rows.Clear();
            this.resetForm();
            dslop = m_LopCtrl.Lay_DSLop();
            foreach (LopInfo l in dslop)
            {
                dataGridView_L.Rows.Add(l.MaLop,l.TenLop,l.Chuyen);
            }
            try
            {
                frmQLHS.cbx_DSHS.DataSource = dslop;
                frmQLHS.cbx_DSHS.ValueMember = "MaLop";
                frmQLHS.cbx_DSHS.DisplayMember = "TenLop";
            }
            catch { }
 
        }

        private void frmQLLop_Load(object sender, EventArgs e)
        {
            loadDSLop();

        }

        private void btn_L_Sua_Click(object sender, EventArgs e)
        {
            LopInfo lop = new LopInfo(txt_L_Ma.Text, txt_L_Ten.Text, txt_L_Chuyen.Text);
            m_LopCtrl.SuaLop(lop);
            resetForm();
            loadDSLop();


        }

        private void dataGridView_L_SelectionChanged(object sender, EventArgs e)
        {
            int index;
            if (dataGridView_L.SelectedRows.Count <= 0 || dataGridView_L.SelectedRows[0].Index == dataGridView_L.RowCount-1)
            {
               return;
            }
            else
            {
                index = dataGridView_L.SelectedRows[0].Index;
            }
           // dataGridView_L.DefaultCellStyle.SelectionBackColor = Color.Red;
            DataGridViewRow dr = dataGridView_L.Rows[index];
            this.txt_L_Ma.Text = dr.Cells["MaLop"].Value.ToString();
            this.txt_L_Ten.Text = dr.Cells["TenLop"].Value.ToString();
            this.txt_L_Chuyen.Text = dr.Cells["Chuyen"].Value.ToString();

        }

        private void btn_L_Refesh_Click(object sender, EventArgs e)
        {
            loadDSLop();
            resetForm();
            this.txt_L_Ma.Text = m_LopCtrl.getAutoID();
        }

        private void btn_L_Xoa_Click(object sender, EventArgs e)
        {
            String malop = this.txt_L_Ma.Text;
            m_LopCtrl.XoaLop(malop);
            loadDSLop();
        }
    }
}